package lpt;
import java.io.*;
import java.net.*;

public class SoftwareLpt extends Thread {

	private BufferedReader in;
	private BufferedWriter out;
	private byte b;

	public SoftwareLpt() {
		Socket sock = null;
		boolean server = false;
		try {
			sock = new Socket(InetAddress.getLocalHost(), 5555);

		} catch (IOException e) {
			server = true;
		}
		if (server) {
			ServerSocket s;
			try {
				s = new ServerSocket(5555);
				sock = s.accept();
			} catch (IOException e) {
				System.err.println("Big error");
				System.exit(-1);
			}
		}
		try {
			this.in = new BufferedReader(new InputStreamReader(
					sock.getInputStream()));
			this.out = new BufferedWriter(new OutputStreamWriter(
					sock.getOutputStream()));
		} catch (IOException e) {
			System.err.println("Big error");
			System.exit(-1);
		}
		start();
	}

	public void run() {
		while (true) {
			String s = null;
			try {
				s = in.readLine();
			} catch (IOException e) {
				System.err.println("Big error");
				System.exit(-1);
			}
			byte temp = Byte.parseByte(s);
			switch (temp) {
			case (0):
				this.b = 0;
				break;
			case (1):
				this.b = 32;
				break;
			case (2):
				this.b = 16;
				break;
			case (3):
				this.b = 48;
				break;
			}
		}
	}

	public void writeLPT(int arg0) {
		byte b = (byte) (arg0 % 4);
		String s = b + "\n";
		try {
			out.write(s);
			out.flush();
		} catch (IOException e) {
			System.err.println("Big error");
			System.exit(-1);
		}
	}

	public byte readLPT() {
		return b;
	}
}
